<?php

namespace App\Http\Controllers;

use App\Http\Requests\GlobalValidation;
use App\Services\BaseRequest;
use App\Services\ElasticSearch;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class SearchController extends Controller
{

    public function searchGoods(BaseRequest $request){
          $keyword=$request->keyword;
          $ela=ElasticSearch::getInstance();
          $data=$ela->searchIndex('goods',$keyword,'_doc','goods_name');
          $data=$data['hits']['hits'];
          $array=[];
          foreach ($data as $k=>$v){
              $data[$k]=$v['_source'];
          }
          $array=[];
          foreach ($data as $k=>$v){
              $array[$k]['description']='';
              $array[$k]['discount_price']='';
              $array[$k]['for_theme_img']='';
              $array[$k]['id']=$v['id'];
              $array[$k]['img']=$v['goods_image'];
              $array[$k]['max_purchase_quantity']='';
              $array[$k]['min_purchase_quantity']='';
              $array[$k]['price']=$v['goods_price'];
              $array[$k]['sketch_spec_id']='';
              $array[$k]['subtitle']=$v['goods_describe'];
              $array[$k]['title']=$v['goods_name'];
          }
          //储存热门词
           Redis::zIncrBy('keywordList',1,$keyword);
          return ['code'=>200,'data'=>$array,'msg'=>'成功'];
        }
        //获取热门词
        public function keyword(){
                $keyword=Redis::zRevRange('keywordList',0,9);
               return ['code'=>200,'data'=>$keyword,'msg'=>'成功'];
        }
}
